<template>
	<view class="auth">
		<view class="nav">
			<uni-nav-bar title="实人认证" backgroundColor="transparent" left-icon="left" @clickLeft="goback">
				<block slot="right">
					<text @click="onSave" style="font-size: 28rpx;">保存</text>
				</block>
			</uni-nav-bar>
		</view>

		<view class="userinfo">
			<view class="list flex_r_between">
				<text>姓名</text>
				<input type="text" v-model="real_name" placeholder="请输入真实姓名" :disabled="status == 2">
			</view>
			<view class="list no_border flex_r_between">
				<text>身份证号</text>
				<input type="text" v-model="id_card" placeholder="请输入身份证号" :disabled="status == 2">
			</view>
			<view class="list no_border flex_r_between">
				<text>手机号码</text>
				<input type="text" v-model="mobile" placeholder="请输入手机号码" :disabled="status == 2">
			</view>
		</view>

		<view class="carmea">
			<view class="title"><text>*</text>请拍幕摄本人实拍照片</view>
			<view class="block flex_r_between">
				<view class="left flex_c">
					<view class="poa_img">
						<image src="/static/technician/photo.png"></image>
						<view class="poa_text">示例</view>
					</view>
					<text>拍摄要求</text>
					<view>正面、免冠、素颜、清晰光线良好、背景整洁</view>
				</view>
				<view class="right flex_r_around">
					<view class="mengb flex_c"
						v-if="authInfo.auth_image.status == 1 || authInfo.auth_image.status == 3">
						<view class="icon" @click.stop="deleteWork">
							<uni-icons type="clear" :size="30" color="#FFF"></uni-icons>
						</view>
						<!-- status 1 审核中
						status 2 通过
						status 3 未通过 -->
						<view>{{authInfo.auth_image.status == 3 ? '审核未通过' : '审核中'}}</view>
						<text>{{authInfo.auth_image.status == 3 ? '需重新传到位工服照片' : '请耐心等待'}}</text>
					</view>
					<image class="auth_img" :src="authInfo.auth_image.image" v-if="!Array.isArray(authInfo.auth_image)" @click="chooseImage"></image>
					<image src="/static/technician/add.png" v-else @click="chooseAuth"></image>
				</view>
			</view>
		</view>

		<view class="rich">
			<view class="list">
				<view class="title">什么是实人认证?</view>
				<view class="text">实人认证是指通过姓名、身份证号和实拍照片，核实认证人身份真实性的一种手段。上单平台将严格保护您的隐私，您上传的身份信息仅供平台认证使用，不会泄露给任何第三方。
				</view>
			</view>
			<!-- <view class="list">
				<view class="title">什么是实人认证?</view>
				<view class="text">实人认证是指通过姓名、身份证号和实拍照片，核实认证人身份真实性的一种手段。上单平台将严格保护您的隐私，您上传的身份信息仅供平台认证使用，不会泄露给任何第三方。</view>
			</view> -->
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				authInfo: '',
				
				real_name:'',
				id_card:'',
				mobile:'',
			}
		},
		methods: {
			goback() {
				uni.navigateBack()
			},
			deleteWork(){
				this.authInfo.auth_image = []
			},
			chooseAuth() {
				this.$util.chooseImage({
					count:1,
					crop:{
						width:800,
						height:800
					}
				},res => {
					uni.showLoading({
						title: '上传头像中',
					})
					this.$util.upload('picture', res.tempFilePaths[0], result=> {
						if (result.code == 0) {
							let obj = {
								auth_image:JSON.stringify({
									image : result.data.url,
									status :this.status,
								})
							}
							
							this.request.httpTokenRequest({
								url: "/technician.technician/saveAuthInfo",
								method: "post"
							}, obj).then(data => {
								uni.hideLoading()
								this.$util.msg(data.msg)
								if (data.code == 0) {
									this.authInfo.auth_image = {
										image:result.data.url,
										status:this.status
									}
								}
							}, error => {})
						}
					},1)
				})
			},
			
			chooseImage(){
				// if(this.status == 1 && this.status == 2){
				// 	return
				// }
				this.$util.chooseImage({
					count:1,
					crop:{
						width:800,
						height:800
					}
				},res => {
					uni.showLoading({
						title: '上传头像中',
					})
					this.$util.upload('picture', res.tempFilePaths[0], result=> {
						if (result.code == 0) {
							let obj = {
								auth_image:JSON.stringify({
									image : result.data.url,
									status :this.status,
								})
							}
							this.request.httpTokenRequest({
								url: "/technician.technician/saveAuthInfo",
								method: "post"
							}, obj).then(data => {
								uni.hideLoading()
								this.$util.msg(data.msg)
								if (data.code == 0) {
									this.authInfo.auth_image = {
										image:result.data.url,
										status:this.status
									}
								}
							}, error => {})
						}
					},1)
				})
			},
			onSave(){
				if(!this.real_name || this.real_name.length < 2){
					return this.$util.msg('请输入真实姓名')
				}
				if(!this.$util.idcardVerify(this.id_card)){
					return this.$util.msg('请输入正确的身份证号')
				}
				if(!this.mobile || this.mobile.length!==11){
					return this.$util.msg('请输入正确的手机号')
				}
				this.request.httpTokenRequest({
					url: "/technician.technician/saveAuthInfo",
					method: "post"
				}, {
					real_name:this.real_name,
					id_card:this.id_card,
					mobile:this.mobile
				}).then(data => {
					uni.hideLoading()
					if (data.code == 0) {
						this.$util.modal(data.msg)
					}else {
						this.$util.msg(data.msg)
					}
				}, error => {})
			},
		},
		onLoad() {
			this.authInfo = uni.getStorageSync('authInfo')
			this.status = this.authInfo.auth_image.status ? this.authInfo.auth_image.status : 1
			this.real_name = this.authInfo.real_name
			this.id_card = this.authInfo.id_card
			this.mobile = this.authInfo.mobile
		}
	}
</script>

<style lang="scss" scoped>
	.mengb {
		width: 300rpx;
		height: 300rpx;
		background: rgba(0, 0, 0, 0.4);
		position: absolute;
		left: 0;
		top: 0;
		padding: 100rpx 0;
		justify-content: space-between;
		z-index: 2;
	
		.icon {
			position: absolute;
			right: 10rpx;
			top: 25rpx;
		}
	
		view {
			font-size: 28rpx;
			color: #FFFFFF;
			line-height: 40rpx;
		}
	
		text {
			font-size: 22rpx;
			color: #FFFFFF;
			line-height: 30rpx;
		}
	}
	.nav {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		z-index: 99;
		padding-top: var(--status-bar-height);
		background: #F5F7F8;
	}

	.auth {
		min-height: 100vh;
		box-sizing: border-box;
		padding: calc(var(--status-bar-height) + 54px) 30rpx 20rpx;
	}

	.userinfo {
		background: #FFFFFF;
		border-radius: 14rpx;
		padding: 0 30rpx;

		.list {
			padding: 30rpx 0;
			border-bottom: 1rpx solid #E6E6E6;

			text {
				font-size: 30rpx;
				color: #08090A;
				line-height: 42rpx;
				margin-right: 30rpx;
			}

			input {
				flex: 1;
				font-size: 30rpx;
				color: #08090A;
				line-height: 42rpx;
			}
		}

	}
	.poa_img {
		position: relative;
		.poa_text {
			position: absolute;
			bottom: 10rpx;
			right: 17rpx;
			font-size: 24rpx;
			color: #FFFFFF !important;
			line-height: 33rpx;
		}
	}
	.carmea {
		background: #FFFFFF;
		border-radius: 14rpx;
		padding: 30rpx;
		margin: 30rpx 0;

		.title {
			font-size: 30rpx;
			color: #08090A;
			line-height: 42rpx;
			margin-bottom: 29rpx;

			text {
				color: #FF0000;
			}
		}

		.block {
			.left {
				flex: 1;

				image {
					width: 200rpx;
					height: 200rpx;
				}

				text {
					font-size: 30rpx;
					font-weight: 600;
					color: #333333;
					line-height: 42rpx;
					margin: 13rpx 0 10rpx;
				}

				view {
					font-size: 22rpx;
					color: #333333;
					line-height: 30rpx;
					white-space: pre-wrap;
					text-align: center;
				}
			}

			

			.right {
				width: 300rpx;
				height: 300rpx;
				border-radius: 20rpx;
				overflow: hidden;
				border: 1rpx solid #D3D3D3;
				margin-left: 24rpx;
				position: relative;
				
				.auth_img {
					width: 300rpx;
					height: 300rpx;
					border-radius: 20rpx;
				}

				image {
					width: 40rpx;
					height: 40rpx;
				}
			}
		}
	}

	.rich {
		.list {
			margin-bottom: 30rpx;

			.title {
				font-size: 28rpx;
				font-weight: 600;
				color: #333333;
				line-height: 40rpx;
				margin-bottom: 20rpx;
			}

			.text {
				font-size: 22rpx;
				color: #333333;
				line-height: 30rpx;
			}
		}
	}
</style>